package com.chinasofti.project.service.itemCategory.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinasofti.project.common.pageQuery.PageResult;
import com.chinasofti.project.dal.mapper.custom.BzBookProductMapperCustom;
import com.chinasofti.project.dal.model.custom.BzProduct;
import com.chinasofti.project.service.itemCategory.ItemCategoryService;
import com.chinasofti.project.service.itemCategory.customForm.SqlSearchForm;

@Service("itemCategoryService")
public class ItemCategoryServiceImpl implements ItemCategoryService {
	@Autowired
	BzBookProductMapperCustom bzBookProductMapperCustom;
	private static Logger logger = LoggerFactory.getLogger(ItemCategoryServiceImpl.class);

	// 搜索
	@Override
	public PageResult<BzProduct> selectByProduct(SqlSearchForm sqlSearchForm) {
		if ((sqlSearchForm.getSearchName()).isEmpty() || (sqlSearchForm.getSearchName()) == null) {
			return search(sqlSearchForm);
		} else if (!(sqlSearchForm.getSearchName().isEmpty() && sqlSearchForm.getSearchName() == null)) {
			return search(sqlSearchForm);
		}
		return null;
	}

	// 详情
	@Override
	public BzProduct selectByProductId(int ProductId) {
		return bzBookProductMapperCustom.selectByProductId(ProductId);
	}

	// 搜索查询共同代码整合方法
	private PageResult<BzProduct> search(SqlSearchForm sqlSearchForm) {
		List<BzProduct> bzProductSum = bzBookProductMapperCustom.selectByProduct(sqlSearchForm);
		int totalCount = bzProductSum.size();
		sqlSearchForm.setPageSize(20);
		long pageSize = sqlSearchForm.getPageSize();
		long pageIdxs = (sqlSearchForm.getPageIdx() - 1) * pageSize;
		sqlSearchForm.setStartLimit(pageIdxs);
		sqlSearchForm.setEndLimit(pageSize);
		List<BzProduct> bzProducts = bzBookProductMapperCustom.selectByProduct(sqlSearchForm);
		return new PageResult<>(totalCount, pageSize, bzProducts);
	}

}
